package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.LocationClass;
import cn.wolfcode.crm.query.LocationClassQueryObject;
import cn.wolfcode.crm.service.ILocationClassService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("locationClass")
public class LocationClassController {
    @Autowired
    public ILocationClassService locationClassService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"locationClass:view", "教室列表"}, logical = Logical.OR)
    public String view() {
        return "locationClass/locationClass";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<LocationClass> selectAll() {
        return locationClassService.selectAll();
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(LocationClassQueryObject qo) {

        return locationClassService.query(qo);
    }

    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"locationClass:saveOrUpdate", "教室增加或编辑"}, logical = Logical.OR)
    public JSONResult saveOrUpdate(LocationClass locationClass) {
        try {
            if (locationClass.getId() != null) {
                locationClassService.updateByPrimaryKey(locationClass);
            } else {
                locationClassService.insert(locationClass);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value = {"locationClass:changeState", "教室停用或再用"}, logical = Logical.OR)
    public JSONResult changeState(Long id) {
        try {
            locationClassService.changState(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"locationClass:delete", "教室删除"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            locationClassService.deleteByPrimaryKey(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("删除失败");
        }
    }
}
